package com.buss.admin.controller;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.xml.crypto.Data;

import org.jweb.core.bean.PageMode;
import org.jweb.core.query.hibernate.qbc.CriteriaQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.buss.activity.trans.vo.ActivedetailVO;
import com.buss.admin.service.impl.ActiveServiceimpl;
import com.buss.admin.trans.form.ActivityForm;
import com.buss.common.entity.HxActive;
import com.buss.common.entity.HxActiveDetail;
import com.buss.common.model.json.EasyUIDataGridResult;
import com.buss.common.model.json.HXResult;

@Controller
@RequestMapping("admin/activemanager")
public class ActiveController {
	
	@Autowired
	private ActiveServiceimpl activityServer;

	@RequestMapping("/active/list")
	@ResponseBody
	public EasyUIDataGridResult getMemberList(ActivityForm searchForm, HttpServletRequest request) {
		// 分页查询
		CriteriaQuery cq = searchForm.getCriteriaQuery();
		int pageNo = Integer.parseInt(request.getParameter("page").toString());
		int pageSize = Integer.parseInt(request.getParameter("rows").toString());
		// 定义返回数据格式
		EasyUIDataGridResult result = new EasyUIDataGridResult();
		PageMode<HxActive> pageMode = activityServer.list4page(cq, pageNo, pageSize);
		
		List<HxActive> uas = pageMode.getResult();
		List<ActivedetailVO> articleVOs = new ArrayList<>();
		ActivedetailVO articleVO = null;
		for(HxActive ua : uas) {
			articleVO = new ActivedetailVO();
			articleVO.copyEntity(ua);
			articleVOs.add(articleVO);
		}
		// 封装数据
		result.setRows(articleVOs);
		result.setTotal(pageMode.getTotal());
		return result;
	}
	
	@RequestMapping(value="/active/add", method={RequestMethod.GET, RequestMethod.POST})
	@ResponseBody
	public HXResult getAdd(HxActive active, String content ) {
//		active.setId(163l);
//		active.setPublishTime(new Date(aa));
		try {
			if(active.getPid()!=null) {
				HxActive hxa = activityServer.get(HxActive.class, active.getPid());
				active.setActive(hxa);
			}
			Date datatime  = new Date();
			active.setCreateTime(datatime);
			active.setUpdateTime(datatime);
			HxActiveDetail detail = new HxActiveDetail();
			detail.setContent(content);
			detail.setCreateTime(datatime);
			detail.setUpdateTime(datatime);
			detail.setLevelclass(active.getLevelclass());
			active.setActiveDetail(detail);
			activityServer.saveOrUpdate(active);
//			active.setPid(active.getPid());
//			activityServer.updateEntitie(active);
			return HXResult.ok("添加成功");
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return HXResult.build(500, "添加失败");
		}
	}
	
	@RequestMapping(value="/active/edit", method={RequestMethod.GET, RequestMethod.POST})
	@ResponseBody
	public HXResult editActive(HxActive active, String content ) {
		try {
			Date datatime = new Date();
			active.setUpdateTime(datatime);
			HxActiveDetail detail = new HxActiveDetail();
			detail.setContent(content);
			detail.setUpdateTime(datatime);
			active.setActiveDetail(detail);
			activityServer.saveOrUpdate(active);
			return HXResult.ok("修改成功");
		} catch (Exception e) {
			
			e.printStackTrace();
			return HXResult.build(500, "修改失败");
		}
	}
	
	@RequestMapping("/active/delete")
	@ResponseBody
	public HXResult delete(String ids, HttpServletRequest request) {
		try {
			String[] idStrings = ids.split(",");
			for (String id : idStrings) {
				activityServer.deleteEntityById(HxActive.class, Long.parseLong(id));
			}

			return HXResult.ok("删除活动成功！");
		} catch (Exception e) {
			e.printStackTrace();
			return HXResult.build(500, "服务器发生错误，删除活动数据失败！");
		}
	}
	
	/*
	 * 管理员相关页面跳转
	 */
	@RequestMapping("/{path}")
	public String goPath(@PathVariable String path, HttpServletRequest request) {
		String pid = request.getParameter("pid");
		if(pid!=null && !pid.equals("")) {
			request.setAttribute("pid", pid);
		}
		return "admin/active/" + path;
	}
}
